# -*- coding: utf-8 -*-
import time
from setting import QUEUE_NAME
from crawler.utils.Singleton import Singleton
from crawler.utils.func_tools import byte_to_str
from crawler.connection.RabbitMq import RabbitMQConnection

__all__ = ['GetTask']


@Singleton
class GetTask(RabbitMQConnection):
    def __init__(self):
        self.queue_name = QUEUE_NAME
        self.channel = self.get_channel()

    def get_task_info(self):
        try:
            res = self.get_task(queue_name=self.queue_name, channel=self.channel)
            if not res:
                time.sleep(5)
                return
            task, current_num = res
            task_info = {"task_info": byte_to_str(task), 'current_num': current_num}
            return task_info
        except None:
            pass

# if __name__ == '__main__':
#     count = 1
#     ct = GetTask()
#     while True:
#         print(ct.get_task_info())
#         count += 1
